package com.nuoniu.sibanyun.service.cm.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.cm.CmContractStage;
import com.nuoniu.sibanyun.entity.cm.CmContractStageGroup;
import com.nuoniu.sibanyun.mapper.cm.CmContractStageGroupMapper;
import com.nuoniu.sibanyun.service.cm.ICmContractStageGroupService;
import com.nuoniu.sibanyun.service.cm.ICmContractStageService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 合同阶段组 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2021-10-20
 */
@Service
public class CmContractStageGroupServiceImpl extends ServiceImpl<CmContractStageGroupMapper, CmContractStageGroup> implements ICmContractStageGroupService {

    @Autowired
    private ICmContractStageService stageService;

    @Override
    public List<CmContractStage> getList(CmContractStageGroup contractStage) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", userInfo.getCompanyId());
        if(StringUtils.isNotBlank(contractStage.getCode())) {
            wrapper.likeRight("code", contractStage.getCode());
        }
        return list(wrapper);
    }

    @Override
    public Long saveGroup(CmContractStageGroup stageGroup) {
        UserInfo userInfo = UserThreadLocal.get();
        stageGroup.setCompanyId(userInfo.getCompanyId());
        CmContractStageGroup groupByName = baseMapper.getByName(stageGroup);
        if(groupByName != null && (groupByName.getId() == null || !groupByName.getId().equals(stageGroup.getId()))) {
            throw new NuoNiuException("名称已存在");
        }

        CmContractStageGroup groupByCode = baseMapper.getByCode(stageGroup);
        if(groupByCode != null && (groupByCode.getId() == null || !groupByCode.getId().equals(stageGroup.getId()))) {
            throw new NuoNiuException("编码已存在");
        }

        this.saveOrUpdate(stageGroup);

        //保存阶段列表
        List<Long> detailIds = new ArrayList<>();
        stageGroup.getContractStageList().forEach(c -> {
            c.setGroupId(stageGroup.getId().intValue());
            c.setCompanyId(userInfo.getCompanyId());
            if(c.getId() != null) {
                detailIds.add(c.getId());
            }
        });

        QueryWrapper deleteWrapper = new QueryWrapper();
        deleteWrapper.eq("company_id", userInfo.getCompanyId());
        deleteWrapper.eq("group_id", stageGroup.getId());
        if(detailIds.size() > 0) {
            deleteWrapper.notIn("id", detailIds);
        }

        stageService.remove(deleteWrapper);
        stageService.saveOrUpdateBatch(stageGroup.getContractStageList());
        return stageGroup.getId();
    }

    @Override
    public void deleteById(Long id) {
        removeById(id);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("group_id", id);
        stageService.remove(wrapper);
    }

    @Override
    public CmContractStageGroup getDetail(Long id) {
        CmContractStageGroup stageGroup = this.getById(id);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("group_id", id);
        stageGroup.setContractStageList(stageService.list(wrapper));
        return stageGroup;
    }
}
